package com.lang.datahandler.service;

import cn.hutool.core.lang.Assert;
import cn.hutool.extra.spring.SpringUtil;
import com.lang.enums.DataSourceEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

/**
 * creat by SimonLang
 * 2022/12/30 19:44
 */
public class HandlerFactory {
    private static final String SUFFIX = "Handler";

    public static DatasourceHandler<?> getHandlerByType(String type){
        Assert.isTrue(!StringUtils.isBlank(type),"数据源名称为空或者不正确");
        return SpringUtil.getBean(type.toLowerCase()+SUFFIX,DatasourceHandler.class);
    }

    public static DatasourceHandler<?> getHandlerByCode(Integer code){
        String type = DataSourceEnum.getEnumByCode(code).getType();
        return getHandlerByType(type);
    }

}

